var ctx;
var firstLevel = 1;
var secondLevel = 2;
var province = '';
var city = '';
var district = '';

function setCtx(ctx) {
	this.ctx = ctx;
}

//验证区域名
function checkName(){
	var name = $("#name").val();
	//解决ie9以下placeholder与value重复的问题
	if(name == $("#name").attr("placeholder")){
		name = "";
	}
	
	if(name == null || name == ""){
		layer.tips('区域名不能为空!', '#name', {
		    tips: 3,
		    time:1000
		});
		return false;
	}else{
		return true;
	}
}
/*
//验证小区名
function checkCellName(){
	var cellName = $("#cellid").val();
	if(cellName == null || cellName == ""){
		layer.tips('请选择小区!', '#cellid', {
			tips: 3,
		    time:1000
		});
		return false;
	}
	return true;
}*/

//验证排序不能为空
function checkSort(){
	var reg = /^[0-9]*[1-9][0-9]*$/;
	var sort = $("#sort").val();
	if(sort == null || sort == ""){
		layer.tips('排序不能为空!', '#sort', {
			tips: 3,
			time:1000
		});
		return false;
	}else if(!reg.test(sort)){
		layer.tips('请输入正整数!', '#sort', {
			tips: 3,
			time:1000
		});
		return false;
	}else{
		return true;
	}
}

//新增或编辑小区区域(1:新增 2:编辑)
var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
function saveOrUpdateCellRegion (){

	var id;
	var name = $("#name").val();
	var memo = $("#memo").val();
	var sort = $("#sort").val();
	if($("#cellRegionId").val() != null && $("#cellRegionId").val() != ''){
		id = $("#cellRegionId").val();
	}else{
		id = 0;
		var cellid = $("#cellid").val();
	}
	
	if(checkName() && checkSort()){
		var index1 = layer.load(1);
		$.post(ctx+"/cellRegion/saveOrUpdateCellRegion",
				{
			id:id,
			cellid:cellid,
			name:name,
			memo:memo,
			sort:sort
				},function(data){
					data = JSON.parse(data);
					if(data.success){
						layer.msg("操作成功",{
							icon: 1,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						},function(){
							parent.loadContent("/cellRegion/cellRegionList");
							parent.layer.close(index);
						});
					} else {
						layer.close(index1);
						layer.msg(data.msg,{
							icon: 2,
							time: 2000 //2秒关闭（如果不配置，默认是3秒）
						});
					}
				});
	}
}

